package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.base.ResultMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.FormFieldService;
import com.symone.webcms.model.FormField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *   @description : 表单字段管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-31
 */
@RestController
@Api(value="/formField", description="表单字段管理")
@RequestMapping("/api/form/field")
public class FormFieldController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(FormFieldController.class);

    @Autowired
    public FormFieldService formFieldService;

    /**
     * @description : 获取表单字段分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-31
     */
    @RequiresPermissions("extend:form:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取表单字段分页列表", notes="获取表单字段分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Integer pageSize,Integer pageNum) {
        Wrapper<FormField> ew = new EntityWrapper<FormField>();
        ew.where("deleted=0");

        //logger.debug("SQL:"+ew.originalSql());

        Page<FormField> page = new Page<FormField>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = formFieldService.findPage(page, ew);

        return ResultMessage.success("获取表单字段的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有表单字段记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-31
     */
    @RequiresPermissions("extend:form:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有表单字段信息", notes="获取所有表单字段信息")
    public ResultMessage list(String code) {
        List<FormField> fields = formFieldService.selectList(new EntityWrapper<FormField>().eq("code" , code).orderBy("sorting" , true));
        return ResultMessage.success("获取所有表单字段信息",fields);
    }

    /**
     * @description : 通过id获取表单字段
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-31
     */
    @RequiresPermissions("extend:form:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取表单字段信息", notes="获取表单字段信息")
    @ApiImplicitParam(name="id",value="表单字段ID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取表单字段信息成功!",formFieldService.selectById(id));
    }

    /**
     * @description : 添加表单字段
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-31
     */
    @RequiresPermissions("extend:form:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value="添加表单字段信息", notes="添加表单字段信息")
    @ApiImplicitParam(name = "表单字段", value = "表单字段信息", required = true )
    public ResultMessage add(FormField formField){
        int count = formFieldService.selectCount(new EntityWrapper<FormField>()
                .eq("code",formField.getCode())
                .and()
                .eq("name",formField.getName())
        );
        if(count > 0){
            return ResultMessage.fail("表单字段重复!");
        }
        formField.setCreateUser(getSessionUser().getUserName());
        formFieldService.insert(formField);
        return ResultMessage.success("添加表单字段成功!");
    }

    /**
     * @description : 通过id逻辑删除表单字段
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-31
     */
    @RequiresPermissions("extend:form:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除表单字段", notes="逻辑删除表单字段")
    @ApiImplicitParam(name = "id", value = "表单字段Id", required = true )
    public ResultMessage removeFlag(Long id){
        if(formFieldService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑表单字段删除成功！");
        } else {
            return ResultMessage.fail("逻辑表单字段删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除表单字段
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-31
     */
    @RequiresPermissions("extend:form:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除表单字段", notes="物理删除表单字段")
    @ApiImplicitParam(name = "id", value = "表单字段Id", required = true )
    public ResultMessage remove(long id){
        if(formFieldService.deleteById(id)) {
            return ResultMessage.success("物理删除表单字段成功！");
        } else {
            return ResultMessage.fail("物理删除表单字段失败！");
        }
    }

    /**
     * @description : 更新表单字段信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-31
     */
    @RequiresPermissions("extend:form:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value="更新表单字段信息", notes="更新表单字段信息")
    @ApiImplicitParam(name = "formField", value = "表单字段信息", required = true )
    public ResultMessage update(FormField formField) {
        int count = formFieldService.selectCount(new EntityWrapper<FormField>()
                .eq("code",formField.getCode())
                .and()
                .eq("name",formField.getName())
                .and()
                .ne("id",formField.getId())
        );
        if(count > 0){
            return ResultMessage.fail("表单字段重复!");
        }
        formField.setUpdateUser(getSessionUser().getUserName());
        if(formFieldService.updateById(formField)) {
            return ResultMessage.success("表单字段信息更新成功!");
        }else{
            return ResultMessage.fail("表单字段信息更新失败!");
        }
    }

    /**
    * @description : 清除表单字段记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-31
    */
    @RequiresPermissions("extend:form:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除表单字段记录", notes="清除已被逻辑删除的表单字段记录")
    public ResultMessage clear(){
        int num = formFieldService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有表单字段记录被清除！");
        }
    }



}
